package chat.client;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

import chat.ui.GroupChatWindow;
import chat.user.Group;
import chat.user.Message;
import chat.user.Request;

public class ClientRequestor implements Runnable {

	public ObjectOutputStream oos;
	public GroupChatWindow group_chat;
	public String user_name;
	private Socket s;

	public ClientRequestor(Socket socket) throws Exception {

		s = socket;

	}

	@Override
	public void run() {

		try {

			for (;; Thread.sleep(500)) {    //used to improve performance

				// while (true) {

				try {

					/*
					 * Old code for (; ClientMain.getOutMessageCount()>0; ){
					 */
					while (ClientMain.getOutMessageCount() > 0) {

						Request r = ClientMain.getOutMessage();

						/*
						 * ObjectOutputStream oos = new ObjectOutputStream( new
						 * BufferedOutputStream(s.getOutputStream()));
						 */
						oos = new ObjectOutputStream(new BufferedOutputStream(
								s.getOutputStream()));

						oos.writeObject(r);
						oos.flush();

						ClientMain.removeOutMessage(r);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public GroupChatWindow getChat() {
		return this.group_chat;
	}

}
